<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册 - CursorTask</title>
    <script src="/assets/js/api.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            background: #ffffff;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .register-container {
            width: 100%;
            max-width: 900px;
            padding: 24px;
        }

        .register-card {
            display: flex;
            background: white;
            border-radius: 16px;
            box-shadow: 0 4px 24px rgba(0, 0, 0, 0.08);
            overflow: hidden;
            transition: all 0.3s ease;
        }

        .register-image {
            flex: 1;
            background: linear-gradient(135deg, #4776E6 0%, #8E54E9 100%);
            padding: 40px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            color: white;
            text-align: center;
        }

        .register-content {
            flex: 1;
            min-width: 420px;
            padding: 32px;
        }

        .register-header {
            text-align: center;
            margin-bottom: 32px;
        }

        .register-header h1 {
            font-size: 24px;
            color: #2d3436;
            margin-bottom: 8px;
        }

        .register-header p {
            color: #636e72;
            font-size: 14px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-label {
            display: block;
            font-size: 14px;
            color: #2d3436;
            margin-bottom: 8px;
            font-weight: 500;
        }

        .form-input {
            width: 100%;
            padding: 12px 16px;
            border: 1px solid #e1e4e8;
            border-radius: 8px;
            font-size: 14px;
            transition: all 0.3s ease;
            background: #f8f9fa;
        }

        .form-input:focus {
            outline: none;
            border-color: #4776E6;
            background: white;
            box-shadow: 0 0 0 3px rgba(71, 118, 230, 0.1);
        }

        .register-button {
            width: 100%;
            padding: 12px;
            background: linear-gradient(135deg, #4776E6 0%, #8E54E9 100%);
            border: none;
            border-radius: 8px;
            color: white;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-top: 24px;
        }

        .register-button:hover {
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(71, 118, 230, 0.2);
        }

        .register-button:disabled {
            background: #cbd5e0;
            cursor: not-allowed;
            transform: none;
        }

        .login-link {
            text-align: center;
            margin-top: 24px;
            color: #636e72;
            font-size: 14px;
        }

        .login-link a {
            color: #4776E6;
            text-decoration: none;
            font-weight: 500;
        }

        .error-message {
            color: #ef4444;
            font-size: 12px;
            margin-top: 4px;
        }

        .message-box {
            position: fixed;
            top: 20px;
            left: 50%;
            transform: translateX(-50%);
            padding: 12px 24px;
            border-radius: 8px;
            color: white;
            font-size: 14px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
            z-index: 1000;
            transition: all 0.3s ease;
        }

        .message-success { background-color: #10b981; }
        .message-error { background-color: #ef4444; }
    </style>
</head>
<body>
    <div class="register-container">
        <div class="register-card">
            <div class="register-image">
                <img src="assets/images/bg.svg" alt="注册插图" style="width: 100%; max-width: 320px; margin-bottom: 24px;">
                <h2>欢迎加入 CursorTask</h2>
                <p>注册账号以使用完整功能</p>
            </div>

            <div class="register-content">
                <div class="register-header">
                    <h1>创建账号</h1>
                    <p>填写以下信息完成注册</p>
                </div>

                <form id="registerForm" onsubmit="return false;">
                    <div class="form-group">
                        <label class="form-label">用户名</label>
                        <input type="text" class="form-input" name="username" required 
                               placeholder="请输入用户名">
                    </div>

                    <div class="form-group">
                        <label class="form-label">邮箱</label>
                        <input type="email" class="form-input" name="email" required 
                               placeholder="请输入邮箱">
                    </div>

                    <div class="form-group">
                        <label class="form-label">手机号码</label>
                        <input type="tel" class="form-input" name="phone" required 
                               placeholder="请输入手机号码">
                    </div>

                    <div class="form-group">
                        <label class="form-label">密码</label>
                        <input type="password" class="form-input" name="password" required 
                               placeholder="请输入密码">
                    </div>

                    <div class="form-group">
                        <label class="form-label">确认密码</label>
                        <input type="password" class="form-input" name="confirmPassword" required 
                               placeholder="请再次输入密码">
                    </div>

                    <button type="submit" class="register-button">注册</button>
                </form>

                <div class="login-link">
                    已有账号？ <a href="/">立即登录</a>
                </div>
            </div>
        </div>
    </div>

    <script>
        // 表单验证和提交
        const form = document.getElementById('registerForm');
        
        form.addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const formData = new FormData(form);
            const password = formData.get('password');
            const confirmPassword = formData.get('confirmPassword');
            
            // 验证密码匹配
            if (password !== confirmPassword) {
                showMessage('两次输入的密码不一致', 'error');
                return;
            }

            // 验证邮箱格式
            const email = formData.get('email');
            if (!isValidEmail(email)) {
                showMessage('请输入有效的邮箱地址', 'error');
                return;
            }

            // 验证手机号格式
            const phone = formData.get('phone');
            if (!isValidPhone(phone)) {
                showMessage('请输入有效的手机号码', 'error');
                return;
            }

            try {
                const submitButton = form.querySelector('button[type="submit"]');
                submitButton.disabled = true;
                submitButton.textContent = '注册中...';

                const response = await fetch(`${BASE_URL}/api/auth/register`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        username: formData.get('username'),
                        email: formData.get('email'),
                        phone: formData.get('phone'),
                        password: formData.get('password'),
                        avatar: ''  // 暂时为空
                    })
                });

                const result = await response.json();

                if (response.status === 200) {
                    showMessage('注册成功，即将跳转到登录页面', 'success');
                    setTimeout(() => {
                        window.location.href = '/';
                    }, 2000);
                } else {
                    throw new Error(result.detail || '注册失败，请重试');
                }
            } catch (error) {
                showMessage(error.message, 'error');
            } finally {
                const submitButton = form.querySelector('button[type="submit"]');
                submitButton.disabled = false;
                submitButton.textContent = '注册';
            }
        });

        // 辅助函数
        function isValidEmail(email) {
            return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
        }

        function isValidPhone(phone) {
            return /^1[3-9]\d{9}$/.test(phone);
        }

        function showMessage(message, type = 'info') {
            const existingMessage = document.querySelector('.message-box');
            if (existingMessage) {
                existingMessage.remove();
            }

            const messageBox = document.createElement('div');
            messageBox.className = `message-box message-${type}`;
            messageBox.textContent = message;

            document.body.appendChild(messageBox);

            setTimeout(() => {
                messageBox.style.opacity = '0';
                setTimeout(() => messageBox.remove(), 300);
            }, 3000);
        }

        // 实时密码匹配验证
        const passwordInput = form.querySelector('input[name="password"]');
        const confirmInput = form.querySelector('input[name="confirmPassword"]');

        function validatePasswords() {
            if (confirmInput.value && passwordInput.value !== confirmInput.value) {
                confirmInput.setCustomValidity('密码不匹配');
            } else {
                confirmInput.setCustomValidity('');
            }
        }

        passwordInput.addEventListener('input', validatePasswords);
        confirmInput.addEventListener('input', validatePasswords);
    </script>
</body>
</html> 